package com.lv.hechuang.ui.activity;

import android.app.Dialog;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.DownloadBuilder;
import com.allenliu.versionchecklib.v2.builder.UIData;
import com.allenliu.versionchecklib.v2.callback.CustomDownloadingDialogListener;
import com.allenliu.versionchecklib.v2.callback.CustomVersionDialogListener;
import com.lv.baselibrary.util.Utils;
import com.lv.baselibrary.widgets.view.BaseDialog;
import com.lv.hechuang.R;
import com.lv.hechuang.base.BaseActivity;
import com.lv.hechuang.common.CommonBaseUrl;
import com.lv.hechuang.common.CommonData;
import com.lv.hechuang.common.CommonString;
import com.lv.hechuang.retrofithttp.XRetrofitUtils;
import com.lv.hechuang.ui.fragment.MineFragment;
import com.lv.hechuang.ui.fragment.ShopFragment;
import com.lv.hechuang.ui.fragment.VideoFragment;
import com.lv.hechuang.widget.CircularProgressView;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: MainActivity
 * @Description: 主页
 * @Author: lv
 * @Date: 2020/9/3 11:10
 */
public class MainActivity extends BaseActivity {

    private String TAG = "MainActivity";
    private BottomNavigationView nav_view;
    private List<Fragment> fragmentList;
    private int index;//当前显示fragment
    private int[] menuId = {R.id.navigation_home, R.id.navigation_shop, R.id.navigation_exchange, R.id.navigation_mine};
    private RelativeLayout top_status_bar_rl;
    private BaseDialog baseDialog;
    private LinearLayout ll_corner_mark_all;

    @Override
    public void initData(Bundle bundle) {
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(Bundle savedInstanceState, View view) {
        nav_view = fvbi(R.id.nav_view);
        top_status_bar_rl = fvbi(R.id.top_status_bar_rl);
    }

    @Override
    public void doBusiness() {
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager != null) {
            manager.cancelAll();
        }
        setDefaultFragment();
        setNavSelListener();
//        checkVersion();//版本检测
    }


    //版本检测
    private void checkVersion() {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                String authorization = getAuthorization();
                XRetrofitUtils retrofitUtils = new XRetrofitUtils.Builder()
                        .setUrl(CommonBaseUrl.CheckVersion)
                        .setParams("id", CommonData.getAppverID())
                        .build();

                retrofitUtils.AsynPostDevice(authorization, new XRetrofitUtils.OnResultListener() {
                    @Override
                    public void onNetWorkError() {

                    }

                    @Override
                    public void onSuccess(String data) {
                        if (!TextUtils.isEmpty(data)) {
                            Gson gson = new Gson();
                        }
                    }

                    @Override
                    public void onNoData(String data) {

                    }

                    @Override
                    public void onServerError() {

                    }
                });
            }
        }.start();

    }

    //加密head
    private String getAuthorization() {
        String id = CommonData.getAppverID();
        String device_id = Utils.getUniquePsuedoID();
        return Utils.MD5(device_id + "_" + CommonString.KEY + "_" + id);
    }

    //版本检测更新
//    private void upDateVersion(final VersionBean versionBean) {
//        String package_url = versionBean.getPackage_url();
//        if (!TextUtils.isEmpty(package_url)) {
//            DownloadBuilder builder = AllenVersionChecker
//                    .getInstance()
//                    .downloadOnly(UIData.create().setDownloadUrl(package_url));
//            builder.setCustomVersionDialogListener(new CustomVersionDialogListener() {
//                @Override
//                public Dialog getCustomVersionDialog(Context context, UIData versionBundle) {
//                    baseDialog = new BaseDialog(context, R.style.BaseDialog, R.layout.dialog_check_version);
//                    Button versionchecklib_version_dialog_commit = baseDialog.findViewById(R.id.versionchecklib_version_dialog_commit);
//                    Button versionchecklib_version_dialog_cancel = baseDialog.findViewById(R.id.versionchecklib_version_dialog_cancel);
//                    versionchecklib_version_dialog_commit.requestFocus();
//                    versionchecklib_version_dialog_cancel.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            baseDialog.dismiss();
//                        }
//                    });
//                    TextView tv_update_code = baseDialog.findViewById(R.id.tv_update_code);
//                    tv_update_code.setText("软件更新提示 v" + versionBean.getApp_version());
//                    TextView tv_update_info = baseDialog.findViewById(R.id.tv_update_info);
//                    tv_update_info.setText(versionBean.getContent());
//                    baseDialog.setCancelable(false);
//                    if (versionBean.getForce().equals("1")) {
//                        versionchecklib_version_dialog_cancel.setVisibility(View.INVISIBLE);
//                    } else {
//                        versionchecklib_version_dialog_cancel.setVisibility(View.VISIBLE);
//                    }
//                    return baseDialog;
//                }
//            });
//            builder.setCustomDownloadingDialogListener(createCustomDownloadingDialog());
//            builder.setShowNotification(false);
//            builder.executeMission(MainActivity.this);
//        }
//
//    }

    //更新进度
    private CustomDownloadingDialogListener createCustomDownloadingDialog() {
        return new CustomDownloadingDialogListener() {
            @Override
            public Dialog getCustomDownloadingDialog(Context context, int progress, UIData versionBundle) {
                BaseDialog baseDialog = new BaseDialog(context, R.style.BaseDialog, R.layout.dialog_check_version);
                TextView tv_update_code = baseDialog.findViewById(R.id.tv_update_code);
                tv_update_code.setText(getResources().getString(R.string.update_ing));
                baseDialog.findViewById(R.id.versionchecklib_version_dialog_commit).setVisibility(View.INVISIBLE);
                baseDialog.findViewById(R.id.tv_update_info).setVisibility(View.INVISIBLE);
                baseDialog.findViewById(R.id.versionchecklib_version_dialog_cancel).setVisibility(View.INVISIBLE);
                baseDialog.findViewById(R.id.rl_progress_all).setVisibility(View.VISIBLE);
                baseDialog.setCancelable(false);
                return baseDialog;
            }

            @Override
            public void updateUI(Dialog dialog, int progress, UIData versionBundle) {
                CircularProgressView cpv_update_progress = dialog.findViewById(R.id.cpv_update_progress);
                cpv_update_progress.setProgress(progress);
                TextView tv_update_progress_text = dialog.findViewById(R.id.tv_update_progress_text);
                tv_update_progress_text.setText(progress + "%");
            }
        };

    }


    //设置底部导航
    private void setNavSelListener() {
        nav_view.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                for (int i = 0; i < menuId.length; i++) {
                    if (item.getItemId() == menuId[i]) {
                        transaction.hide(fragmentList.get(index));
                        if (!fragmentList.get(i).isAdded()) {
                            transaction.add(R.id.ll_main_fragment, fragmentList.get(i)).show(fragmentList.get(i));
                        } else {
                            transaction.show(fragmentList.get(i));
                        }
                        transaction.commit();
                        index = i;
                        return true;
                    }
                }
                return false;
            }
        });
        View childAt = nav_view.getChildAt(0);
        childAt.findViewById(R.id.navigation_home).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

        childAt.findViewById(R.id.navigation_mine).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) nav_view.getChildAt(0);
//这里就是获取所添加的每一个Tab(或者叫menu)，
        View tab = menuView.getChildAt(1);
        BottomNavigationItemView itemView = (BottomNavigationItemView) tab;
//加载我们的角标View，新创建的一个布局
        nav_view.setItemIconTintList(null);
    }

    //设置默认
    private void setDefaultFragment() {
        fragmentList = new ArrayList<>();
        VideoFragment videoFragment = new VideoFragment();
        ShopFragment shopFragment = new ShopFragment();
        ShopFragment exchangeFragment = new ShopFragment();
        MineFragment mineFragment = new MineFragment();
        fragmentList.add(videoFragment);
        fragmentList.add(shopFragment);
        fragmentList.add(exchangeFragment);
        fragmentList.add(mineFragment);
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.ll_main_fragment, videoFragment).commit();
        nav_view.setVisibility(View.VISIBLE);
        index = 0;
    }

    @Override
    public void onWidgetClick(View view) {

    }
}
